package com.woniuxy.returnQue;

import com.rabbitmq.client.*;
import com.woniuxy.utils.Utils;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class RetuenProducer {
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        //获取连接对象
        Connection connection = Utils.getConnection();
        //对接通道
        Channel channel = connection.createChannel();
        //增加监听器
        channel.addReturnListener(new ReturnListener() {
            @Override
            public void handleReturn(int replyCode, String replyText, String exchange, String routingKey, AMQP.BasicProperties properties, byte[] body) throws IOException {
                //在这个监听方法中 进行处理 没有进入队列的消息
                System.out.println("消息退回");
                System.out.println("replyCode---->"+replyCode);
                System.out.println("replyText---->"+replyText);
                System.out.println("exchange----->"+exchange);
                System.out.println("routingKey---->"+routingKey);
                System.out.println("body------>"+new String(body));
            }
        });
        //发送消息  需要将mandatory设置为true
        channel.basicPublish("returnExchange","return111",true, MessageProperties.PERSISTENT_TEXT_PLAIN,"RetuenProducer 发送消息 ".getBytes());
        TimeUnit.MILLISECONDS.sleep(2000);
        channel.close();
        connection.close();
    }
}
